/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.cell;

import com.sencha.gxt.cell.core.client.form.FieldCell;
import com.sencha.gxt.widget.core.client.event.ColumnWidthChangeEvent;
import com.sencha.gxt.widget.core.client.event.ColumnWidthChangeEvent.ColumnWidthChangeHandler;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;


/**
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <M>
 *           the model type
 */
public class CellColumnResizer<M> implements ColumnWidthChangeHandler
{

	private final ColumnConfig<M, ?> column;
	private final FieldCell<?> cell;
	private int margin;

	/**
	 * Default margin
	 */
	public static final int DEFAULT_MARGIN = 5;

	/**
	 * Constructor with margin = {@value #DEFAULT_MARGIN}
	 * 
	 * @param column
	 * @param cell
	 */
	public CellColumnResizer(final ColumnConfig<M, ?> column, final FieldCell<?> cell)
	{
		this(column, cell, DEFAULT_MARGIN);
	}

	/**
	 * Constructor
	 * 
	 * @param column
	 * @param cell
	 * @param margin
	 */
	public CellColumnResizer(final ColumnConfig<M, ?> column, final FieldCell<?> cell, final int margin)
	{
		this.column = column;
		this.cell = cell;
		this.margin = margin;
	}

	@Override
	public void onColumnWidthChange(final ColumnWidthChangeEvent event)
	{
		if (column == event.getColumnConfig())
		{
			final int w = event.getColumnConfig().getWidth();
			cell.setWidth(w - margin);

			//			for (int i = 0; i < rows; i++)
			//			{
			//				final SupplierDetailData p = grid.getStore().get(i);
			//
			//				// option 1 could be better for force fit where all columns are
			//				// resized
			//				// would need to run deferred using DelayedTask to ensure only run
			//				// once
			//				//					final DelayedTask delayedTask = new DelayedTask()
			//				//					{
			//				//						@Override
			//				//						public void onExecute()
			//				//						{
			//				//							grid.getStore().update(p);
			//				//						}
			//				//					};
			//				//					delayedTask.delay(1);
			//
			//				//					// option 2
			//				Element parent = grid.getView().getCell(i, col);
			//				if (parent != null)
			//				{
			//					parent = parent.getFirstChildElement();
			//					final SafeHtmlBuilder sb = new SafeHtmlBuilder();
			//					cell.render(new Context(i, col, store.getKeyProvider().getKey(p)), column.getValueProvider().getValue(p), sb);
			//					parent.setInnerHTML(sb.toSafeHtml().asString());
			//				}
			//			}
		}
	}

}
